<template>
    <!--地图挂载dom-->
    <div id="map">
        <div id="info">&nbsp;</div>
    </div>
</template>

<script>
    //引入依赖
    import GPX from 'ol/format/GPX';
    import Map from 'ol/Map';
    import VectorSource from 'ol/source/Vector';
    import View from 'ol/View';
    import OSM from 'ol/source/OSM'
    import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
    import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
    export default {
        name: 'GPXLayer',
        data() {
            return {
            }
        },
        mounted() {

            const raster = new TileLayer({
                source: new OSM()//加载OpenStreetMap
            });

            const style = {
                'Point': new Style({
                    image: new CircleStyle({
                        fill: new Fill({
                            color: 'rgba(255,255,0,0.4)',
                        }),
                        radius: 5,
                        stroke: new Stroke({
                            color: '#ff0',
                            width: 1,
                        }),
                    }),
                }),
                'LineString': new Style({
                    stroke: new Stroke({
                        color: '#f00',
                        width: 3,
                    }),
                }),
                'MultiLineString': new Style({
                    stroke: new Stroke({
                        color: '#0f0',
                        width: 3,
                    }),
                }),
            };

            //gpx也是矢量数据，因此也需要使用矢量图层渲染与矢量数据源加载
            const vector = new VectorLayer({
                source: new VectorSource({
                    url: 'http://localhost:8888/openlayer/gpx/fells_loop.gpx',
                    format: new GPX(),//指定加载的是gpx的数据格式
                }),
                style: function (feature) {
                    return style[feature.getGeometry().getType()];
                },
            });

            const map = new Map({
                layers: [raster, vector],
                target: document.getElementById('map'),
                view: new View({
                    center: [-7916041.528716288, 5228379.045749711],
                    zoom: 12,
                }),
            });

            //鼠标移动与鼠标的事件监听，获取到要素信息的描述信息
            const displayFeatureInfo = function (pixel) {
                const features = [];
                map.forEachFeatureAtPixel(pixel, function (feature) {
                    features.push(feature);
                });
                if (features.length > 0) {
                    const info = [];
                    let i, ii;
                    for (i = 0, ii = features.length; i < ii; ++i) {
                        info.push(features[i].get('desc'));
                    }
                    document.getElementById('info').innerHTML = info.join(', ') || '(unknown)';
                    map.getTarget().style.cursor = 'pointer';
                } else {
                    document.getElementById('info').innerHTML = '&nbsp;';
                    map.getTarget().style.cursor = '';
                }
            };

            //鼠标移动事件回调
            map.on('pointermove', function (evt) {
                if (evt.dragging) {
                    return;
                }
                const pixel = map.getEventPixel(evt.originalEvent);
                displayFeatureInfo(pixel);
            });

            //鼠标点击的事件回调
            map.on('click', function (evt) {
                displayFeatureInfo(evt.pixel);
            });
        },
        methods: {
        }
    }
</script>

<style scoped>
    #map {
        width: 100%;
        height: 100%;
    }

    .MapTool {
        position: absolute;
        top: .5em;
        right: .5em;
        z-index: 9999;
    }
</style>
